from sqlalchemy import Column, Integer, VARCHAR, ForeignKey,DateTime
from sqlalchemy.orm import relationship
from models.base_class import Base, gen_uuid
from models.order import Order


class User(Base):
    """
    用户表
    """
    __tablename__ = 'users'
    user_id = Column(VARCHAR(32), default=gen_uuid, unique=True, comment="用户id")
    username = Column(VARCHAR(128), unique=True, index=True, nullable=True, comment="用户名")
    email = Column(VARCHAR(128), unique=True, index=True, nullable=True, comment="邮箱")
    phone = Column(VARCHAR(16), unique=True, index=True, nullable=True, comment="手机号")
    nickname = Column(VARCHAR(128), comment="昵称")
    avatar = Column(VARCHAR(256), comment="头像")
    password = Column(VARCHAR(128), nullable=False, comment="密码")
    is_active = Column(Integer, default=False, comment="邮箱是否激活 0=未激活 1=激活", server_default="0")
    last_login = Column(DateTime, comment="上次登录时间")
    __table_args__ = ({'comment': '用户表'})

    # Relationship to Orders
    orders = relationship('Order', back_populates='user')

    # 示例：
    # 定义一对多关系
    # class User(Base):
    #     # ...
    #     orders = relationship("Order", back_populates="user")
    #
    # class Order(Base):
    #     # ...
    #     user_id = Column(Integer, ForeignKey('user.user_id'))
    #     user = relationship("User", back_populates="orders")

    # 定义一对一关系
    # class User(Base):
    #     # ...
    #     profile = relationship("Profile", uselist=False, back_populates="user")
    #
    # class Profile(Base):
    #     # ...
    #     user_id = Column(Integer, ForeignKey('user.user_id'))
    #     user = relationship("User", back_populates="profile")

    # 定义多对多关系，通过course_association_table表关联
    # class Student(Base):
    #     # ...
    #     courses = relationship("Course", secondary=course_association_table, back_populates="students")
    #
    # class Course(Base):
    #     # ...
    #     students = relationship("Student", secondary=course_association_table, back_populates="courses")
    #
    # course_association_table = Table('course_student', Base.metadata,
    #                                  Column('student_id', Integer, ForeignKey('student.student_id')),
    #                                  Column('course_id', Integer, ForeignKey('course.course_id'))
    #                                  )